package com.wa.graphql.exception;
import graphql.execution.DataFetcherExceptionHandler;
import graphql.execution.DataFetcherExceptionHandlerParameters;
import graphql.execution.ExecutionPath;
import graphql.language.SourceLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description GraphQL自定义异常处理
 * @Author Dawn
 * @Date 2021/7/8 15:00
 */
public class CustomDataFetcherExceptionHandler implements DataFetcherExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomDataFetcherExceptionHandler.class);

    @Override
    public void accept(DataFetcherExceptionHandlerParameters handlerParameters) {
        Throwable exception = handlerParameters.getException();
        SourceLocation sourceLocation = handlerParameters.getField().getSourceLocation();
        ExecutionPath path = handlerParameters.getPath();

        handlerParameters.getExecutionContext().addError(new CustomExceptionDataFetching(path, exception, sourceLocation));
        LOGGER.warn("=================CustomDataFetcherExceptionHandler==出现异常：{}", exception);
    }
}
